/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#include <QMessageBox>
#include <QSqlError>
#include "emiterefacturi.h"
#include "database.h"
#include "kontamabilloader.h"
#include "menuwidgetinterface.h"
#include "dataemitere.h"

using namespace SQL;

EmitereFacturi::EmitereFacturi(QWidget* parent, Qt::WFlags fl)
		: KontAMabilWindow(parent, fl), Ui::emitereFacturiForm()
{
	setupUi(this);
}

EmitereFacturi::~EmitereFacturi()
{
}

void EmitereFacturi::on_cursMoneda_clicked()
{
	foreach(KontAMabil::MenuWidgetInterface* menu, kontAMabil()->menuPlugins())
		if (dynamic_cast<KontAMabil::MenuWidgetInterface*>(menu)->name()=="valute")
		{
			KontAMabilWindow * window=dynamic_cast<KontAMabil::MenuWidgetInterface*>(menu)->createWidget();
			window->exec();
			window->deleteLater();
			break;
		}
}

void EmitereFacturi::on_contracteFacturare_clicked()
{
	foreach(KontAMabil::MenuWidgetInterface* menu, kontAMabil()->menuPlugins())
		if (dynamic_cast<KontAMabil::MenuWidgetInterface*>(menu)->name()=="facturarecontracte")
		{
			KontAMabilWindow * window=dynamic_cast<KontAMabil::MenuWidgetInterface*>(menu)->createWidget();
			window->exec();
			window->deleteLater();
			break;
		}
}

void EmitereFacturi::on_emitereFacturi_clicked()
{
	DataEmitere de;
	if (de.exec()!=QDialog::Accepted)
		return;

	QSqlQuery qry(defaultDatabase()->database());
	qry.prepare("select * from emite_facturi(:luna, :anul, :seria, :numarul, :data);");
	qry.bindValue(":luna",de.dataEmitere->date().month());
	qry.bindValue(":anul",de.dataEmitere->date().year());
	qry.bindValue(":seria",de.seria->text());
	qry.bindValue(":numarul",de.numarul->value());
	qry.bindValue(":data",de.dateEmit->date());
	if (qry.exec())
		QMessageBox::information(this,"Succes emitere facturi","Emiterea facturilor s-a incheiat cu success.\nPentru a tipari facturile urmati meniul \"Facturare/Rapoarte/Listare Facturi\"");
	else
		QMessageBox::information(this,"Eroare emitere facturi",QString("Emiterea facturilor a esuat.\nServerul a intors mesajul:\n <<%1>>").arg(qry.lastError().text()));
}
